# 构建阶段
FROM golang:1.22-alpine AS builder

# 设置工作目录
WORKDIR /app

# 安装必要工具
RUN apk add --no-cache git make

# 设置GOPROXY以解决依赖下载超时问题
ENV GOPROXY=https://goproxy.cn,direct

# 复制依赖文件
COPY go.mod go.sum ./
# 下载依赖
RUN go mod download

# 复制源代码
COPY . .

# 使用Makefile构建
RUN make build

# 运行阶段
FROM alpine:3.19

# 创建非root用户
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

# 安装运行时依赖
RUN apk add --no-cache ca-certificates tzdata

# 设置时区
ENV TZ=Asia/Shanghai

# 设置工作目录
WORKDIR /app

# 复制配置文件
COPY --from=builder /app/etc /app/etc

# 复制编译好的二进制文件
COPY --from=builder /app/build/bin/api-server /app/api-server

# 设置权限
RUN chown -R appuser:appgroup /app

# 切换到非root用户
USER appuser

# 暴露端口
EXPOSE 8888

# 设置健康检查
HEALTHCHECK --interval=30s --timeout=3s --retries=3 CMD wget -q --spider http://localhost:8888/health || exit 1

# 启动应用
ENTRYPOINT ["/app/api-server", "-f", "/app/etc/api.yaml"] 